//==================================================================
//  Copyright (C) 2006-2007  Davide Pasca
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//==================================================================
/// @file	psys_string.h
///
/// Basic OS services.
///
//==================================================================

#ifndef PSYS_STRING_H
#define PSYS_STRING_H

#include <tchar.h>
#include <string>
#include <xstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <limits>

//==================================================================
namespace PSYS {

//==================================================================
#define PSYS_MAX_PATH	2048

//==================================================================
void StrRemoveBeginendSpaces( char *s );
void TStrRemoveBeginendSpaces( TCHAR *s );
int TStrFindLastOf( const TCHAR *s, TCHAR find_ch );
void TStrRemoveUntilLastOf( TCHAR *s, TCHAR find_ch );
TCHAR *MByteToTCHAR( u_int mbtype, const char *in_ansip );
TCHAR *ANSIToTCHAR( const char *in_ansip );
TCHAR *UTF8ToTCHAR( const char *in_utf8p );
char *TCHARToMByte( u_int mbtype, const TCHAR *in_tcharp );
char *TCHARToANSI( const TCHAR *in_tcharp );
char *TCHARToUTF8( const TCHAR *in_tcharp );
int FTPrintF_S_UTF8( FILE *stream, const TCHAR *format, ... );
int VFTPrintF_S_UTF8( FILE *stream, const TCHAR *format, va_list va );

//=============================================================================
typedef std::basic_string<TCHAR> tstring;

typedef std::basic_ostringstream<TCHAR, std::char_traits<TCHAR>,
	std::allocator<TCHAR> > otstringstream;

//=============================================================================
template<typename T> inline tstring Stringify(const T& x)
{
	otstringstream out;
	out << x;
	return out.str();
}

template<> inline tstring Stringify<bool>(const bool& x)
{
	otstringstream out;
	out << std::boolalpha << x;
	return out.str();
}

template<> inline tstring Stringify<double>(const double& x)
{
	const int sigdigits = std::numeric_limits<double>::digits10;
	otstringstream out;
	out << std::setprecision(sigdigits) << x;
	return out.str();
}

template<> inline tstring Stringify<float>(const float& x)
{
	const int sigdigits = std::numeric_limits<float>::digits10;
	otstringstream out;
	out << std::setprecision(sigdigits) << x;
	return out.str();
}

template<> inline tstring Stringify<long double>(const long double& x)
{
	const int sigdigits = std::numeric_limits<long double>::digits10;
	otstringstream out;
	out << std::setprecision(sigdigits) << x;
	return out.str();
}

tstring TStrFilenameFromPath( const tstring &s );
const TCHAR *TStrGetFilenameExtension( const TCHAR *fnamep );

//==================================================================
};

#endif